<template>
    <div class="status_bar">
        <div class="status_info">
            <span>帧率:{{ Math.floor(statusInfo.fps) }}FPS 经度:{{ (statusInfo.position[0]).toFixed(5) }}° 纬度:{{
                (statusInfo.position[1]).toFixed(5) }}° 高度:{{
                (statusInfo.position[2]).toFixed(2) }}米 偏航角:{{ (statusInfo.rotation[0]).toFixed(2) }}° 俯仰角:{{
                (statusInfo.rotation[1]).toFixed(2) }}° 翻滚角:{{
                (statusInfo.rotation[2]).toFixed(2) }}°</span>
            <span @dblclick="copyClipboard(realTimeInfo)">{{ realTimeInfo ? `鼠标位置:${realTimeInfo[0].toFixed(5)}°
                ${realTimeInfo[1].toFixed(5)}°
                ${realTimeInfo[2].toFixed(2)}m` : '暂时无法获取鼠标位置...' }}</span>
        </div>
        <!-- <div>
            <span>EarthUI版本 : {{ '24.07.01' }}</span>
            <span class="earthui_version">{{ version }}</span>
        </div> -->
    </div>
</template>
<script setup lang="ts">
import { inject, onMounted, onBeforeUnmount, ref } from "vue";
import { copyright } from "../../../copyright";
import { XbsjEarthUi } from '../../../scripts/xbsjEarthUi';
import { createAnimateFrameWithStartValues } from 'xbsj-xe2/dist-node/xe2-utils';
const xbsjEarthUi = inject('xbsjEarthUi') as XbsjEarthUi
async function copyClipboard(text: string) {//复制
    navigator.clipboard.writeText(text)
        .then(function () {
            console.log('复制成功');
        }, function (e) {
            console.log('复制失败');
        });
}
const version = ref('Cesium 1.116')
const statusInfo = ref({ fps: 0, position: [0, 0, 0], rotation: [0, 0, 0], length: 0 })
const realTimeInfo = ref()
const debugData = async () => {
    const viewer = xbsjEarthUi.activeViewer
    if (!viewer) return
    statusInfo.value.fps = await viewer.getFPS()
    const cameraInfo = await viewer.getCurrentCameraInfo()
    if (cameraInfo&&cameraInfo.position&&cameraInfo.rotation) {
        statusInfo.value.position = cameraInfo.position
        statusInfo.value.rotation = cameraInfo.rotation
    }
}
let dispose: any
let animateFrame: any
let animateFrame2: any
const viewePosRot = () => {
    const viewer = xbsjEarthUi.activeViewer
    if (!viewer) return
    if (viewer.innerViewer.typeName === 'CzmViewer') {
        setTimeout(() => {
            //@ts-ignore
            viewer.innerViewer.extensions.cursorPositionInfo.enabled = true
        }, 1000)
        animateFrame2 = createAnimateFrameWithStartValues(() => {
            //@ts-ignore
            const mousePosition = viewer.innerViewer.extensions?.cursorPositionInfo.cursorPosition
            realTimeInfo.value = mousePosition
        });
        animateFrame2.start()
    } else {
        dispose = viewer.hoverEvent.disposableOn((res) => {
            viewer.pickPosition(res.screenPosition).then(res => {
                realTimeInfo.value = res
            })
        })
    }
}
const destroy = () => {
    if (animateFrame) {
        animateFrame.destroy()
        animateFrame = undefined
    }
    if (animateFrame2) {
        animateFrame2.destroy()
        animateFrame2 = undefined
    }
    if (dispose) {
        dispose()
        dispose = undefined
    }
}
const getVersion = () => {
    const viewer = xbsjEarthUi.activeViewer
    if (viewer) {
        setTimeout(() => {
            version.value = viewer.getVersion()[5] ?? 'Cesium 1.116'
        }, 1000)
    }
}
onMounted(() => {
    getVersion()
    debugData()
    animateFrame = createAnimateFrameWithStartValues(debugData);
    animateFrame.start();
    viewePosRot()
    xbsjEarthUi.dispose(xbsjEarthUi.activeViewerChanged.disposableOn(res => {
        destroy()
        debugData()
        animateFrame = createAnimateFrameWithStartValues(debugData);
        animateFrame.start();
        viewePosRot()
        getVersion()
    }))
})
onBeforeUnmount(() => {
    destroy()
});
</script>
<style scoped>
.status_bar {
    width: 100%;
    position: fixed;
    height: 30px;
    left: 0;
    bottom: 0;
    color: #fff;
    padding: 0 20px 0 0;
    box-sizing: border-box;
    line-height: 30px;
    z-index: 100;
    display: flex;
    align-content: center;
    justify-content: space-between;
    background: rgba(37, 38, 42, 0.8);
    font-size: 14px;
}

.status_info {
    display: flex;
}

.earthui_version {
    margin-left: 20px;
}
</style>